package com.agentlab.imitation.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.agentlab.imitation.bean.DimZxdm;
import com.agentlab.imitation.bean.User;
import com.agentlab.imitation.bean.admin.Admin;
import com.agentlab.imitation.service.admin.AdminService;
import com.agentlab.imitation.service.base.DaoSupport;
import com.agentlab.imitation.util.MD5;

@Service @Transactional
public class DimZxdmServiceBean  extends DaoSupport<DimZxdm> implements DimZxdmService {
	@Resource SessionFactory factory;
	@SuppressWarnings("unchecked")
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public  String[] getZxdq() {
		// TODO Auto-generated method stub
		 
		 List<String> list = factory.getCurrentSession().createQuery("select distinct dqmc from DimZxdm ")
		 .setCacheable(true).setCacheRegion("cache_zxdqmc").list();
		 String[] zsdq = new String[list.size()];
		 int i = 0;
         for(String name : list){
             //System.out.println(name);
             zsdq[i] = name;
              i++;
         }
		return zsdq;
	}
	
	/**
	 * 得到中学代码表所有记录对应的map，用于级联下拉选择
	 * 用到缓存
	 */
	@SuppressWarnings("unchecked")
	public HashMap getMap(){
		HashMap<String, String[]> zsdmMap = new HashMap<String, String[]>();
		String[] zsdqlist= getZxdq();
		for(int i=0;i<zsdqlist.length;i++){
			List<String> list = factory.getCurrentSession().
				createQuery("select zxmc  from DimZxdm a where a.dqmc = :dqmc ")
				.setParameter("dqmc", zsdqlist[i]).setCacheable(true)
				.setCacheRegion("cache_zxmcmap").list();
			String[] zxmcTemp = new String[list.size()];
			int j=0;
			for(String name : list){
				zxmcTemp[j++] = name;
			}
			zsdmMap.put(zsdqlist[i], zxmcTemp);
		}
		return zsdmMap;
	}

	public void delete(Serializable... entityids) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("unchecked")
	public DimZxdm getZxByZxmc(String zxmc) {
		// TODO Auto-generated method stub
		List<DimZxdm> list = factory.getCurrentSession().
		createQuery(" from DimZxdm a where a.zxmc = :zxmc ")
		.setParameter("zxmc", zxmc).setCacheable(true)
		.setCacheRegion("cache_DimZxdm").list();
		DimZxdm zx = list.get(0);
		return zx;
	}

	
	
	
	
	}
